/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.analyzer.gui.component;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.ImageUtilities;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import pl.tom.social.dal.SocialDatabaseFacade;
import pl.tom.social.dal.entity.Site;
import pl.tom.social.dal.entity.SiteEntry;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//pl.tom.social.analyzer.gui.component//Site//EN",
autostore = false)
public final class SiteTopComponent extends TopComponent {

	private static final Logger logger = Logger.getLogger(SiteTopComponent.class);
	private static SiteTopComponent instance;
	/** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
	private static final String PREFERRED_ID = "SiteTopComponent";
	private Site site;

	public SiteTopComponent() {
		initComponents();
		setName(NbBundle.getMessage(SiteTopComponent.class, "CTL_SiteTopComponent"));
		setToolTipText(NbBundle.getMessage(SiteTopComponent.class, "HINT_SiteTopComponent"));
//        setIcon(ImageUtilities.loadImage(ICON_PATH, true));
	}

	public SiteTopComponent(String siteUrl) {
		this();
		if(siteUrl != null) {
			SocialDatabaseFacade db = new SocialDatabaseFacade();
			try {
				this.site = db.getSite(siteUrl);
			} catch(Exception ex) {
				logger.log(Level.ERROR, ex);
			}
		}
	}

	public SiteTopComponent(Site site) {
		this();
		this.site = site;
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        siteSplitPane = new javax.swing.JSplitPane();
        siteInfoTopPanel = new javax.swing.JPanel();
        siteInfoPanel = new javax.swing.JPanel();
        siteUrlLabel = new javax.swing.JLabel();
        siteAddedLabel = new javax.swing.JLabel();
        siteUpdateLabel = new javax.swing.JLabel();
        siteEntriesNrLabel = new javax.swing.JLabel();
        siteOldestEntryLabel = new javax.swing.JLabel();
        siteNewestEntryLabel = new javax.swing.JLabel();
        siteUrlInfo = new javax.swing.JLabel();
        siteAddedInfo = new javax.swing.JLabel();
        siteUpdateInfo = new javax.swing.JLabel();
        siteEntriesNrInfo = new javax.swing.JLabel();
        siteOldestEntryInfo = new javax.swing.JLabel();
        siteNewestEntryInfo = new javax.swing.JLabel();
        siteOwnerPanel = new javax.swing.JPanel();
        siteLinksPanel = new javax.swing.JPanel();
        siteLinksScrollPane = new javax.swing.JScrollPane();
        siteLinksList = new javax.swing.JList();
        siteEntriesPanel = new javax.swing.JPanel();
        entriesScrollPane = new javax.swing.JScrollPane();
        entriesList = new javax.swing.JList();
        entryInfoPanel = new javax.swing.JPanel();
        entryUrlLabel = new javax.swing.JLabel();
        entryTitleLabel = new javax.swing.JLabel();
        entryAddedLabel = new javax.swing.JLabel();
        entryCommentsNrLabel = new javax.swing.JLabel();
        entryUrlInfo = new javax.swing.JLabel();
        entryTitleInfo = new javax.swing.JLabel();
        entryAddedInfo = new javax.swing.JLabel();
        entryCommentsNrInfo = new javax.swing.JLabel();

        setAutoscrolls(true);
        setLayout(new java.awt.BorderLayout());

        siteInfoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteInfoPanel.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteUrlLabel, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteUrlLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteAddedLabel, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteAddedLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteUpdateLabel, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteUpdateLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntriesNrLabel, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteEntriesNrLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteOldestEntryLabel, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteOldestEntryLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteNewestEntryLabel, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteNewestEntryLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteUrlInfo, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteUrlInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteAddedInfo, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteAddedInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteUpdateInfo, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteUpdateInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntriesNrInfo, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteEntriesNrInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteOldestEntryInfo, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteOldestEntryInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteNewestEntryInfo, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteNewestEntryInfo.text")); // NOI18N

        javax.swing.GroupLayout siteInfoPanelLayout = new javax.swing.GroupLayout(siteInfoPanel);
        siteInfoPanel.setLayout(siteInfoPanelLayout);
        siteInfoPanelLayout.setHorizontalGroup(
            siteInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(siteInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(siteInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(siteInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteUrlLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 291, Short.MAX_VALUE)
                        .addComponent(siteUrlInfo))
                    .addGroup(siteInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteAddedLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 258, Short.MAX_VALUE)
                        .addComponent(siteAddedInfo))
                    .addGroup(siteInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteUpdateLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 247, Short.MAX_VALUE)
                        .addComponent(siteUpdateInfo))
                    .addGroup(siteInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteEntriesNrLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 218, Short.MAX_VALUE)
                        .addComponent(siteEntriesNrInfo))
                    .addGroup(siteInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteOldestEntryLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 244, Short.MAX_VALUE)
                        .addComponent(siteOldestEntryInfo))
                    .addGroup(siteInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteNewestEntryLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 239, Short.MAX_VALUE)
                        .addComponent(siteNewestEntryInfo)))
                .addContainerGap())
        );
        siteInfoPanelLayout.setVerticalGroup(
            siteInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(siteInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(siteInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteUrlLabel)
                    .addComponent(siteUrlInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(siteInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteAddedLabel)
                    .addComponent(siteAddedInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(siteInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteUpdateLabel)
                    .addComponent(siteUpdateInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(siteInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteEntriesNrLabel)
                    .addComponent(siteEntriesNrInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(siteInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteOldestEntryLabel)
                    .addComponent(siteOldestEntryInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(siteInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteNewestEntryLabel)
                    .addComponent(siteNewestEntryInfo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        siteOwnerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteOwnerPanel.border.title"))); // NOI18N

        javax.swing.GroupLayout siteOwnerPanelLayout = new javax.swing.GroupLayout(siteOwnerPanel);
        siteOwnerPanel.setLayout(siteOwnerPanelLayout);
        siteOwnerPanelLayout.setHorizontalGroup(
            siteOwnerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 328, Short.MAX_VALUE)
        );
        siteOwnerPanelLayout.setVerticalGroup(
            siteOwnerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 68, Short.MAX_VALUE)
        );

        siteLinksPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteLinksPanel.border.title"))); // NOI18N

        siteLinksList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        siteLinksList.setEnabled(false);
        siteLinksScrollPane.setViewportView(siteLinksList);

        javax.swing.GroupLayout siteLinksPanelLayout = new javax.swing.GroupLayout(siteLinksPanel);
        siteLinksPanel.setLayout(siteLinksPanelLayout);
        siteLinksPanelLayout.setHorizontalGroup(
            siteLinksPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(siteLinksPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(siteLinksScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE)
                .addContainerGap())
        );
        siteLinksPanelLayout.setVerticalGroup(
            siteLinksPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(siteLinksPanelLayout.createSequentialGroup()
                .addComponent(siteLinksScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout siteInfoTopPanelLayout = new javax.swing.GroupLayout(siteInfoTopPanel);
        siteInfoTopPanel.setLayout(siteInfoTopPanelLayout);
        siteInfoTopPanelLayout.setHorizontalGroup(
            siteInfoTopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(siteInfoTopPanelLayout.createSequentialGroup()
                .addGroup(siteInfoTopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(siteOwnerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(siteInfoPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(siteLinksPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        siteInfoTopPanelLayout.setVerticalGroup(
            siteInfoTopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(siteInfoTopPanelLayout.createSequentialGroup()
                .addComponent(siteInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(siteOwnerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(siteLinksPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        siteSplitPane.setLeftComponent(siteInfoTopPanel);

        siteEntriesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.siteEntriesPanel.border.title"))); // NOI18N

        entriesList.setModel(new DefaultListModel());
        entriesScrollPane.setViewportView(entriesList);

        entryInfoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.entryInfoPanel.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(entryUrlLabel, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.entryUrlLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(entryTitleLabel, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.entryTitleLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(entryAddedLabel, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.entryAddedLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(entryCommentsNrLabel, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.entryCommentsNrLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(entryUrlInfo, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.entryUrlInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(entryTitleInfo, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.entryTitleInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(entryAddedInfo, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.entryAddedInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(entryCommentsNrInfo, org.openide.util.NbBundle.getMessage(SiteTopComponent.class, "SiteTopComponent.entryCommentsNrInfo.text")); // NOI18N

        javax.swing.GroupLayout entryInfoPanelLayout = new javax.swing.GroupLayout(entryInfoPanel);
        entryInfoPanel.setLayout(entryInfoPanelLayout);
        entryInfoPanelLayout.setHorizontalGroup(
            entryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(entryInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(entryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(entryInfoPanelLayout.createSequentialGroup()
                        .addComponent(entryUrlLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 378, Short.MAX_VALUE)
                        .addComponent(entryUrlInfo))
                    .addGroup(entryInfoPanelLayout.createSequentialGroup()
                        .addComponent(entryTitleLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 371, Short.MAX_VALUE)
                        .addComponent(entryTitleInfo))
                    .addGroup(entryInfoPanelLayout.createSequentialGroup()
                        .addComponent(entryAddedLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 345, Short.MAX_VALUE)
                        .addComponent(entryAddedInfo))
                    .addGroup(entryInfoPanelLayout.createSequentialGroup()
                        .addComponent(entryCommentsNrLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 290, Short.MAX_VALUE)
                        .addComponent(entryCommentsNrInfo)))
                .addContainerGap())
        );
        entryInfoPanelLayout.setVerticalGroup(
            entryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(entryInfoPanelLayout.createSequentialGroup()
                .addGroup(entryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(entryUrlLabel)
                    .addComponent(entryUrlInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(entryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(entryTitleLabel)
                    .addComponent(entryTitleInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(entryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(entryAddedLabel)
                    .addComponent(entryAddedInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(entryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(entryCommentsNrLabel)
                    .addComponent(entryCommentsNrInfo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout siteEntriesPanelLayout = new javax.swing.GroupLayout(siteEntriesPanel);
        siteEntriesPanel.setLayout(siteEntriesPanelLayout);
        siteEntriesPanelLayout.setHorizontalGroup(
            siteEntriesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, siteEntriesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(siteEntriesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(entriesScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                    .addComponent(entryInfoPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        siteEntriesPanelLayout.setVerticalGroup(
            siteEntriesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, siteEntriesPanelLayout.createSequentialGroup()
                .addComponent(entriesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(entryInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        siteSplitPane.setRightComponent(siteEntriesPanel);

        add(siteSplitPane, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList entriesList;
    private javax.swing.JScrollPane entriesScrollPane;
    private javax.swing.JLabel entryAddedInfo;
    private javax.swing.JLabel entryAddedLabel;
    private javax.swing.JLabel entryCommentsNrInfo;
    private javax.swing.JLabel entryCommentsNrLabel;
    private javax.swing.JPanel entryInfoPanel;
    private javax.swing.JLabel entryTitleInfo;
    private javax.swing.JLabel entryTitleLabel;
    private javax.swing.JLabel entryUrlInfo;
    private javax.swing.JLabel entryUrlLabel;
    private javax.swing.JLabel siteAddedInfo;
    private javax.swing.JLabel siteAddedLabel;
    private javax.swing.JLabel siteEntriesNrInfo;
    private javax.swing.JLabel siteEntriesNrLabel;
    private javax.swing.JPanel siteEntriesPanel;
    private javax.swing.JPanel siteInfoPanel;
    private javax.swing.JPanel siteInfoTopPanel;
    private javax.swing.JList siteLinksList;
    private javax.swing.JPanel siteLinksPanel;
    private javax.swing.JScrollPane siteLinksScrollPane;
    private javax.swing.JLabel siteNewestEntryInfo;
    private javax.swing.JLabel siteNewestEntryLabel;
    private javax.swing.JLabel siteOldestEntryInfo;
    private javax.swing.JLabel siteOldestEntryLabel;
    private javax.swing.JPanel siteOwnerPanel;
    private javax.swing.JSplitPane siteSplitPane;
    private javax.swing.JLabel siteUpdateInfo;
    private javax.swing.JLabel siteUpdateLabel;
    private javax.swing.JLabel siteUrlInfo;
    private javax.swing.JLabel siteUrlLabel;
    // End of variables declaration//GEN-END:variables

	/**
	 * Gets default instance. Do not use directly: reserved for *.settings files only,
	 * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
	 * To obtain the singleton instance, use {@link #findInstance}.
	 */
	public static synchronized SiteTopComponent getDefault() {
		if(instance == null) {
			instance = new SiteTopComponent();
		}
		return instance;
	}

	/**
	 * Obtain the SiteTopComponent instance. Never call {@link #getDefault} directly!
	 */
	public static synchronized SiteTopComponent findInstance() {
		TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
		if(win == null) {
			Logger.getLogger(SiteTopComponent.class.getName()).warn(
				"Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
			return getDefault();
		}
		if(win instanceof SiteTopComponent) {
			return (SiteTopComponent) win;
		}
		Logger.getLogger(SiteTopComponent.class.getName()).warn(
			"There seem to be multiple components with the '" + PREFERRED_ID +
			"' ID. That is a potential source of errors and unexpected behavior.");
		return getDefault();
	}

	@Override
	public int getPersistenceType() {
		return TopComponent.PERSISTENCE_NEVER;
	}

	@Override
	public void componentOpened() {
		if(this.site != null) {
			SocialDatabaseFacade db = new SocialDatabaseFacade();
			this.setSiteInfo(site);
			this.setSiteEntryList(db, site);
			this.setSiteLinksList(db, site);

			entriesList.addListSelectionListener(new ListSelectionListener() {

				public void valueChanged(ListSelectionEvent e) {
					Object o = ((JList) e.getSource()).getSelectedValue();
					if(o instanceof SiteEntry) {
						SiteEntry entry = (SiteEntry) o;
						SocialDatabaseFacade db = new SocialDatabaseFacade();
						entryUrlInfo.setText(entry.getUrl());
						entryTitleInfo.setText(entry.getTitle());
						entryAddedInfo.setText(entry.getAdded().toString());
						entryCommentsNrInfo.setText(db.getSiteEntryCommentsNr(entry).toString());
					}
				}
			});

			entriesList.addMouseListener(new MouseAdapter() {

				@Override
				public void mouseClicked(MouseEvent e) {
					if(e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
						Object o = entriesList.getSelectedValue();
						if(o != null && o instanceof SiteEntry) {
							SiteEntry entry = (SiteEntry) o;
							SiteEntryTopComponent window = new SiteEntryTopComponent(entry);
							window.open();
							window.requestActive();
						}
					}
				}
			});
		} else {
			NotifyDescriptor dialog = new NotifyDescriptor.Message(
				"No site defined for this window. Site is not downloaded or not set properly on window opening.",
				NotifyDescriptor.WARNING_MESSAGE);
			DialogDisplayer.getDefault().notify(dialog);
			this.close();
		}
	}

	@Override
	public void componentClosed() {
		// TODO add custom code on component closing
	}

	void writeProperties(java.util.Properties p) {
		// better to version settings since initial version as advocated at
		// http://wiki.apidesign.org/wiki/PropertyFiles
		p.setProperty("version", "1.0");
	}

	Object readProperties(java.util.Properties p) {
		SiteTopComponent singleton = SiteTopComponent.getDefault();
		singleton.readPropertiesImpl(p);
		return singleton;
	}

	private void readPropertiesImpl(java.util.Properties p) {
		String version = p.getProperty("version");
	}

	@Override
	protected String preferredID() {
		return PREFERRED_ID;
	}

	private void setSiteInfo(Site site) {
		SocialDatabaseFacade db = new SocialDatabaseFacade();
		this.siteUrlInfo.setText(site.getUrl());
		this.siteAddedInfo.setText(site.getAdded().toString());
		if(site.getUpdated() != null) {
			this.siteUpdateInfo.setText(site.getUpdated().toString());
		}
		this.siteEntriesNrInfo.setText(db.getSiteEntriesNr(site).toString());
	}

	private void setSiteEntryList(SocialDatabaseFacade db, Site site) {
		//TODO change to get siteentry urls
		List<SiteEntry> entries = db.getSiteEntries(site);
		if(entries != null && entries.size() > 0) {
			this.siteEntriesNrInfo.setText(String.valueOf(entries.size()));
			this.siteNewestEntryInfo.setText(entries.get(entries.size() - 1).getAdded().toString());
			this.siteOldestEntryInfo.setText(entries.get(0).getAdded().toString());
			this.entriesList.setListData(entries.toArray());
			this.entriesList.repaint();
		}
	}

	private void setSiteLinksList(SocialDatabaseFacade db, Site site) {
		List<String> links = db.getSiteLinksUrls(site);
		if(links != null && !links.isEmpty()) {
			this.siteLinksList.setListData(links.toArray());
		}
	}
}
